/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/binary-tree-inorder-traversal
   @Language: C++
   @Datetime: 19-07-09 17:41
   */

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

// Method 1, recursion
class Solution {
	void inorder(TreeNode *root, vector<int> &v){
		if(root->left) inorder(root->left,v);
		v.push_back(root->val);
		if(root->right) inorder(root->right,v);
	}
public:
	vector<int> inorderTraversal(TreeNode* root) {
		vector<int> v;
		if(root==NULL) return v;
		inorder(root,v);
		return v;
	}
};

// Method 2, loop
class Solution {
public:
	vector<int> inorderTraversal(TreeNode* root) {
		vector<int> v;
		stack<TreeNode*> s;
		for(; root; root=root->left)
			s.push(root);
		for(; s.size();){
			root = s.top();
			s.pop();
			v.push_back(root->val);
			for(root=root->right; root; root=root->left)
				s.push(root);
		}
		return v;
	}
};
